
package com.limoilux.cobolux.ui.console;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleFactory;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IOConsole;

public class CoboluxConsoleFactory implements IConsoleFactory
{
	private final IConsoleManager manager;
	private final IOConsole localConsole;
	
	public CoboluxConsoleFactory()
	{
		ConsolePlugin consolPlugin = ConsolePlugin.getDefault();
		
		this.manager = consolPlugin.getConsoleManager();
		this.localConsole = CoboluxConsole.getConsole();
	}

	@Override
	public void openConsole()
	{

		System.out.println("Adding console");
		if (!this.consoleExists())
		{
			this.addConsole();
		}
	}
	
	public void addConsole()
	{
		IConsole[] consoles = new IConsole[1];

		consoles[0] = CoboluxConsole.getConsole();
		
		this.manager.addConsoles(consoles);
	}
	
	public boolean consoleExists()
	{
		IConsole[] consoles = this.manager.getConsoles();
		boolean found = false;

		for (int i = 0; i < consoles.length && !found; i++)
		{
			found = consoles[i] == this.localConsole;
		}
		
		return found;
	}

}
